/**
 * 
 */
package edu.vanderbilt.vshare.multicast;

import java.io.IOException;
import java.io.OutputStream;

/**
 * class for streaming file packets to a multicast port
 * 
 * @author jedmondson
 *
 */
public class SROutputStream extends OutputStream {

	/**
	 * Constructor for this class
	 */
	public SROutputStream() {
		
	}

	/** setup a new multicast thread to send contents of a certain
	 * file at a certain rate
	 * 
	 * @param    ip             first packet to send
	 * @param    filename       name of the file to multicast
	 * @param    rate           total packets to send (including start)
	 */
	
	public void write (String ip, String filename, String rate)
	{
		boolean success = true;
		
		if (!SharedGlobals.store.contains(filename))
		{
			success = SharedGlobals.store.add(filename);
		}
		
		if (success)
			ServerGlobals.repo.add(ip,filename,rate);
	}
	
	/** send work units to the multicasting threads that are sending
	 * the specified file. The work units will start from the packet
	 * indicated by start and include start and numPackets + 1 successors.
	 * 
	 * @param    filename       name of the file to multicast
	 * @param    start          first packet to send
	 * @param    numPackets     total packets to send (including start)
	 */
	
	public void write (String filename, int start, int numPackets)
	{
		boolean success = true;
		
		if (!SharedGlobals.store.contains(filename))
		{
			success = SharedGlobals.store.add(filename);
		}
		
		if (success)
		{
		
			int fileid = SharedGlobals.store.getFileId(filename);
		
			ServerGlobals.repo.add(fileid, start, numPackets);
		}
	}

	/** send work units to the multicasting threads that are sending
	 * the specified file. The work units will start from the metadata
	 * packet and will continue to the end of the file.
	 * 
	 * @param    filename       name of the file to multicast
	 */
	
	public void write(String filename)
	{
		write (filename, -1, -1);
	}

	/** deprecated and unused. Only included because OutputStream forces
	 * us to
	 */
	@Override
	public void write(int arg0) throws IOException {
		// TODO Auto-generated method stub

	}
}
